package test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;


import javastudy2.Course;
import javastudy2.CourseMapper;
import javastudy2.Student;
import javastudy2.StudentCourse;
import javastudy2.StudentCourseMapper;
import javastudy2.StudentMapper;
import utility.MyBatisUtils;

public class MyTest2 {
	
	@Test
	public void testSelect() throws IOException {
		SqlSession session=MyBatisUtils.openSession();
//		StudentMapper studentMapper=session.getMapper(StudentMapper.class);
//		Student student=studentMapper.get(2);
//		System.out.println(student);
		
		CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		Course course=courseMapper.get(2);
		System.out.println(course);
		session.close();
	}
	
//连接联系
	@Test
	public void testAdd() throws IOException {
		 SqlSession session=MyBatisUtils.openSession();
		 StudentCourseMapper scmp=session.getMapper(StudentCourseMapper.class);
		 StudentMapper studentMapper=session.getMapper(StudentMapper.class);
		 CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		 StudentCourse sc=new StudentCourse();
		 sc.setStudent(studentMapper.get(2));
		 sc.setCourse(courseMapper.get(1));
		 scmp.add(sc);
		 session.commit();
		 session.close();		 
	}

//删除联系
	@Test
	public void testDelete() throws IOException {
		 SqlSession session=MyBatisUtils.openSession();
		 StudentCourseMapper scmp=session.getMapper(StudentCourseMapper.class);
		 StudentMapper studentMapper=session.getMapper(StudentMapper.class);
		 CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		 StudentCourse sc=new StudentCourse();
		 sc.setStudent(studentMapper.get(2));
		 sc.setCourse(courseMapper.get(1));
		 scmp.delete(sc);
		 session.commit();
		 session.close();
	}
	
//四个表连接
	@Test
	public void testFour() throws IOException {
		SqlSession session=MyBatisUtils.openSession();
		CourseMapper courseMapper=session.getMapper(CourseMapper.class);
		Course course=courseMapper.get(2);
		System.out.println(course);
		session.commit();
		session.close();
		
	}
	
}
